home *** CD-ROM | disk | FTP | other *** search
/ mail.altrad.com / 2015.02.mail.altrad.com.tar / mail.altrad.com / TEST / office deutch / INFOPATH.NL-NL / INFLR.CAB / SVCREQ.XSN_1043 / script.js < prev    next >
Text File  |  2006-11-12  |  22KB  |  701 lines

  1. /*
  2.  * This file contains functions for data validation and form-level events.
  3.  * Because the functions are referenced in the form definition (.xsf) file, 
  4.  * it is recommended that you do not modify the name of the function,
  5.  * or the name and number of arguments.
  6.  *
  7. */
  8.  
  9. // The following line is created by Microsoft Office InfoPath to define the
  10. // prefixes for all the known namespaces in the main XML data file.
  11. // Any modification to the form files made outside of InfoPath
  12. // will not be autmatically updated.
  13. //<namespacesDefinition>
  14. XDocument.DOM.setProperty("SelectionNamespaces", 'xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD" xmlns:svc="http://schemas.microsoft.com/office/infopath/2003/sample/ServiceRequest"');
  15. //</namespacesDefinition>
  16.  
  17. /*------------------------------------------------------------------------------
  18.     This function handler is generated and managed automatically.
  19.     Do not rename the function or alter its parameter list.
  20. ------------------------------------------------------------------------------*/
  21. function XDocument::OnLoad(oEvent)
  22. {
  23.     updateDropDown(true /*isOnLoad*/, false /*isUndoRedo*/, XDocument.IsSigned /*isSigned*/);
  24.  
  25.     // Avoid DOM updates when the document has been digitally signed.
  26.     if (XDocument.IsSigned)
  27.         return;
  28.  
  29.     var today = new Date();
  30.     initializeNodeValue("/svc:serviceRequest/svc:dateOpened", getDateString(today));
  31.     return;
  32. }
  33.  
  34. //=======
  35. // The following function handler is created by Microsoft Office InfoPath.
  36. // Do not modify the name of the function or the name and number of arguments. 
  37. //=======
  38. function msoxd__service::OnAfterChange(eventObj)
  39. {
  40.     updateDropDown(false /*isOnLoad*/, eventObj.IsUndoRedo, XDocument.IsSigned);
  41. }
  42.  
  43. function updateDropDown(isOnLoad, isUndoRedo, isSigned)
  44. {  
  45.     //get the auxDOM
  46.     var oScratchDOM = XDocument.GetDOM('map');
  47.     oScratchDOM.setProperty("SelectionNamespaces", 'xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD" xmlns:svc="http://schemas.microsoft.com/office/infopath/2003/sample/ServiceRequest"');
  48.  
  49.     //get the current service selection
  50.     var xmlService = XDocument.DOM.selectSingleNode("/svc:serviceRequest/svc:issue/svc:service");
  51.     var strService = xmlService.nodeTypedValue;
  52.  
  53.     var xmlProblem = XDocument.DOM.selectSingleNode("/svc:serviceRequest/svc:issue/svc:problem");
  54.     var strProblem = xmlService.nodeTypedValue;
  55.  
  56.     if (!isOnLoad && !isUndoRedo && !isSigned)
  57.     {
  58.         //reset the problem selection
  59.         setNodeValue(XDocument.DOM.selectSingleNode("/svc:serviceRequest/svc:issue/svc:problem"),'');
  60.     }
  61.  
  62.     //the rest of the changes only apply to visuals, not the document, so we can make them
  63.     //in order to correctly display the current data
  64.  
  65.     //clean up the scratchDOM
  66.     var oSelection = oScratchDOM.selectNodes("/svc:serviceMap/svc:selectedService/*");
  67.     oSelection.removeAll();
  68.     var xmlTopElement = oScratchDOM.selectSingleNode("/svc:serviceMap/svc:selectedService");
  69.    
  70.  
  71.     var xmlSelectedServiceNode = oScratchDOM.selectSingleNode("/svc:serviceMap/svc:service[@svc:value=" + createStringLiteral(strService) + "]");
  72.     if (xmlSelectedServiceNode)
  73.     {
  74.         xmlTopElement.appendChild(xmlSelectedServiceNode.cloneNode(true));
  75.     }
  76. }
  77.  
  78. function createStringLiteral(strValue)
  79. {
  80.     if (-1 == strValue.indexOf("'"))
  81.         return "'" + strValue + "'";
  82.         
  83.     var rgPieces = strValue.split("'");
  84.     return "concat('" + rgPieces.join("', \"'\", '") + "')";
  85. }
  86. /*------------------------------------------------------------------------------
  87.  
  88.     Common form template functions section - the following code is common to all out-of-box form templates.
  89.  
  90. ------------------------------------------------------------------------------*/
  91.  
  92. /* =============================================================================
  93.     Currency handling
  94. ============================================================================= */
  95.  
  96. /*------------------------------------------------------------------------------
  97.     updateCurrency()
  98. ------------------------------------------------------------------------------*/
  99. function updateCurrency(xmlNode, strPrefix)
  100. {
  101.     var xmlName = xmlNode.selectSingleNode(strPrefix + 'name');
  102.     var xmlSymbol = xmlNode.selectSingleNode(strPrefix + 'symbol');
  103.  
  104.     var strName = xmlName.text;
  105.     var strSymbol = lookupCurrencySymbol(strName);
  106.  
  107.     if (strSymbol)
  108.     {
  109.         setNodeValue(xmlSymbol, '(' + strSymbol + ') ');
  110.     }
  111.     else
  112.     {
  113.         setNodeValue(xmlName, '');
  114.         setNodeValue(xmlSymbol, '');
  115.     }
  116. }
  117.  
  118. /*------------------------------------------------------------------------------
  119.     lookupCurrencySymbol()
  120. ------------------------------------------------------------------------------*/
  121. function lookupCurrencySymbol(strName)
  122. {
  123.     var xmlCurDom = XDocument.GetDOM("currencies");
  124.     var xmlCurrency = xmlCurDom.selectSingleNode("/currencies/currency[@name = '" + strName + "']");
  125.  
  126.     if (xmlCurrency)
  127.         return xmlCurrency.getAttribute("symbol");
  128. }
  129.  
  130. /* =============================================================================
  131.     Date/Time handling
  132. ============================================================================= */
  133.  
  134. /*------------------------------------------------------------------------------
  135.     getDateString()
  136. ------------------------------------------------------------------------------*/
  137. function getDateString(oDate)
  138. {
  139.     // Use today as default value.
  140.     if (oDate == null)
  141.         oDate = new Date();
  142.  
  143.     var m = oDate.getMonth() + 1;
  144.     var d = oDate.getDate();
  145.  
  146.     if (m < 10)
  147.         m = "0" + m;
  148.  
  149.     if (d < 10)
  150.         d = "0" + d;
  151.  
  152.     // ISO 8601 date (YYYY-MM-DD).
  153.     return oDate.getFullYear() + "-" + m + "-" + d;
  154. }
  155.  
  156. /*------------------------------------------------------------------------------
  157.     getTimeString()
  158. ------------------------------------------------------------------------------*/
  159. function getTimeString(oTime)
  160. {
  161.     // Use now as default value.
  162.     if (oTime == null)
  163.         oTime = new Date();
  164.  
  165.     var h = oTime.getHours();
  166.     var m = oTime.getMinutes();
  167.     var s = oTime.getSeconds();
  168.     var ms = oTime.getMilliseconds();
  169.  
  170.     if (h < 10)
  171.         h = "0" + h;
  172.  
  173.     if (m < 10)
  174.         m = "0" + m;
  175.  
  176.     if (s < 10)
  177.         s = "0" + s;
  178.  
  179.     if (ms < 100)
  180.         ms = "0" + (ms < 10 ? "0" : "") + ms;
  181.  
  182.     // ISO 8601 time (HH:MM:SS.SSS).
  183.     return h + ":" + m + ":" + s + "." + ms;
  184. }
  185. /* =============================================================================
  186.     Language conversion operations    
  187. ============================================================================= */
  188.  
  189. /*------------------------------------------------------------------------------
  190.     convertXMLNumberToJScript()
  191. ------------------------------------------------------------------------------*/
  192. function convertXMLNumberToJScript(value)
  193. {
  194.     var retVal;
  195.  
  196.     switch (value)
  197.     {
  198.         case "-INF":
  199.             retVal = Number.NEGATIVE_INFINITY;
  200.             break;
  201.  
  202.         case "INF":
  203.             retVal = Number.POSITIVE_INFINITY;
  204.             break;
  205.  
  206.         case "NaN":
  207.             retVal = NaN;
  208.             break;
  209.  
  210.         default:
  211.             //If no prefix of its argument can be parsed as a
  212.             //float, parseFloat will return NaN. Otherwise, it
  213.             //will return the value of the longest float prefix
  214.             //it can find in its argument.
  215.             retVal = parseFloat(value);
  216.     }
  217.  
  218.     return retVal;
  219. }
  220.  
  221. /*------------------------------------------------------------------------------
  222.     convertJScriptNumberToXML()
  223. ------------------------------------------------------------------------------*/
  224. function convertJScriptNumberToXML(value)
  225. {
  226.     var retVal;
  227.  
  228.     switch (value)
  229.     {
  230.         case Number.NEGATIVE_INFINITY:
  231.             retVal = "-INF";
  232.             break;
  233.  
  234.         case Number.POSITIVE_INFINITY:
  235.             retVal = "INF";
  236.             break;
  237.  
  238.         //NaN == NaN returns false. Any other value is equal to itself,
  239.         //which is why the case below differentiates between NaN and
  240.         //any other valid value
  241.         case value:
  242.             retVal = value;
  243.             break;
  244.  
  245.         default:
  246.             retVal = "NaN";
  247.     }
  248.  
  249.     return retVal;
  250. }
  251.  
  252. /* =============================================================================
  253.     Node value operations
  254. ============================================================================= */
  255.  
  256. /*------------------------------------------------------------------------------
  257.     isInvalidOrEmpty()
  258. ------------------------------------------------------------------------------*/
  259. function isInvalidOrEmpty(xmlNode)
  260. {
  261.     // If there is no value, ignore it.
  262.     if (!xmlNode || !xmlNode.text)
  263.         return true;
  264.  
  265.     // The caller can pass additional error types as optional arguments.
  266.     var aErrorTypes = new Array;
  267.     if (arguments.length > 1)
  268.     {
  269.         for (var i=1; i<arguments.length; i++)
  270.             aErrorTypes.push(arguments[i]);
  271.     }
  272.     else
  273.     {
  274.         aErrorTypes.push("SCHEMA_VALIDATION");
  275.     }
  276.  
  277.     // If there is a validation error related to this node,
  278.     // then the node is invalid.
  279.     for (var i=0; i<XDocument.Errors.Count; i++)
  280.     {
  281.         var oError = XDocument.Errors(i);
  282.  
  283.         if (xmlNode == oError.Node)
  284.         {
  285.             for (var j in aErrorTypes)
  286.             {
  287.                 if (oError.Type == aErrorTypes[j])
  288.                     return true;
  289.             }
  290.         }
  291.     }
  292.  
  293.     // Is valid (no error was found).
  294.     return false;
  295. }
  296.  
  297. /*------------------------------------------------------------------------------
  298.     getNodeValue()
  299. ------------------------------------------------------------------------------*/
  300. function getNodeValue(xpath, defaultValue)
  301. {
  302.     var xmlNode = getNode(xpath);
  303.  
  304.     if (isInvalidOrEmpty(xmlNode))
  305.         return (arguments.length > 1) ? defaultValue : "";
  306.     else
  307.         return xmlNode.text;
  308. }
  309.  
  310. /*------------------------------------------------------------------------------
  311.     getNodeTypedValue()    - Use instead of getNodeValue() if you want
  312.         to parse the value by MSXML according to data type (from schema).
  313.         
  314.     NOTE: this is not really useful for dates in Jscript.
  315. ------------------------------------------------------------------------------*/
  316. function getNodeTypedValue(xpath, defaultValue, fNumber)
  317. {
  318.     var xmlNode = getNode(xpath);
  319.  
  320.     if (isInvalidOrEmpty(xmlNode))
  321.         return (arguments.length > 1) ? defaultValue : "";
  322.     
  323.     if (fNumber)
  324.         return convertXMLNumberToJScript(xmlNode.nodeTypedValue);
  325.     else
  326.         return xmlNode.nodeTypedValue;
  327. }
  328.  
  329. /*------------------------------------------------------------------------------
  330.     setNodeValue()
  331. ------------------------------------------------------------------------------*/
  332. function setNodeValue(xpath, value)
  333. {
  334.     var xmlNode = getNode(xpath);
  335.  
  336.     if (!xmlNode)
  337.         return;
  338.  
  339.     // The xsi:nil needs to be removed before we set the value.
  340.     if (value != "" && xmlNode.getAttribute("xsi:nil"))
  341.         xmlNode.removeAttribute("xsi:nil");
  342.  
  343.     // Setting the value would mark the document as dirty.
  344.     // Let's do that if the value has really changed.
  345.     if (xmlNode.text != value)
  346.         xmlNode.text = value;
  347. }
  348.  
  349. /*------------------------------------------------------------------------------
  350.     setNodeTypedValue() - Use instead of setNodeValue() if you want
  351.         to format the value by MSXML according to data type (from schema).
  352.         
  353.         Use this when setting floating point and decimal values. These
  354.         values are formatted differently in some locales and will cause schema
  355.         validation errors when set using setNodeValue or a node's text property.
  356.         
  357.     NOTE: this is not really useful for dates in Jscript.
  358. ------------------------------------------------------------------------------*/
  359. function setNodeTypedValue(xpath, value)
  360. {
  361.     var xmlNode = getNode(xpath);
  362.     
  363.     if (!xmlNode)
  364.         return;
  365.  
  366.     // The xsi:nil needs to be removed before we set the value.
  367.     if (value != "" && xmlNode.getAttribute("xsi:nil"))
  368.         xmlNode.removeAttribute("xsi:nil");
  369.  
  370.     var convertedValue = convertJScriptNumberToXML(value);
  371.  
  372.     // Setting the value would mark the document as dirty.
  373.     // Let's do that if the value has really changed.
  374.     if (xmlNode.nodeTypedValue != convertedValue)
  375.         xmlNode.nodeTypedValue = convertedValue;
  376. }
  377.  
  378. /*------------------------------------------------------------------------------
  379.     setRoundedValue() - Rounds the value to the default number of decimal
  380.     places and calls setNodeTypedValue() with the result.
  381. ------------------------------------------------------------------------------*/
  382. function setRoundedValue(xpath, value)
  383. {
  384.     var xmlNode = getNode(xpath);
  385.  
  386.     if (!xmlNode)
  387.         return;
  388.  
  389.     var roundedValue = roundFloat(value, 3);
  390.     setNodeTypedValue(xpath, roundedValue);
  391. }
  392.  
  393. /*------------------------------------------------------------------------------
  394.     setNil() - Empty a nillable element.
  395. ------------------------------------------------------------------------------*/
  396. function setNil(xpath)
  397. {
  398.     var xmlNode = getNode(xpath);
  399.  
  400.     if (!xmlNode || xmlNode.text === "")
  401.         return;
  402.  
  403.     // Create xsi:nil attribute with the proper namespace.
  404.     var xmlNil = xmlNode.ownerDocument.createNode(2, "xsi:nil", "http://www.w3.org/2001/XMLSchema-instance");
  405.     xmlNil.text = "true";
  406.  
  407.     // The order is important.
  408.     xmlNode.text = "";
  409.     xmlNode.setAttributeNode(xmlNil);
  410. }
  411.  
  412. /*------------------------------------------------------------------------------
  413.     roundFloat() - Rounds the value to the specified number of decimal places.
  414. ------------------------------------------------------------------------------*/
  415. function roundFloat(value, decimalPlaces)
  416. {
  417.     if (value < -1E15 || 1E15 < value)
  418.     {
  419.         return value;
  420.     }
  421.     else
  422.     {
  423.         var nPowerToRound = Math.pow(10, decimalPlaces);
  424.         return Math.round(value*nPowerToRound)/nPowerToRound;
  425.     }
  426. }
  427.  
  428. /*------------------------------------------------------------------------------
  429.     normalizeSource() - Ensures that we always get a node of type NODE_ELEMENT
  430.         or NODE_ATTRIBUTE.
  431. ------------------------------------------------------------------------------*/
  432. function normalizeSource(oEvent)
  433. {
  434.     // Return the node's parent if the node is of type NODE_TEXT and its parent
  435.     // is of type NODE_ELEMENT
  436.     if (oEvent.Parent.nodeType == 1 && oEvent.Source.nodeType == 3)
  437.         return oEvent.Parent;
  438.         
  439.     // Return the node if it is of type NODE_ELEMENT or NODE_ATTRIBUTE;
  440.     if (oEvent.Source.nodeType == 1 || oEvent.Source.nodeType == 2)
  441.         return oEvent.Source;
  442. }
  443.  
  444. /*------------------------------------------------------------------------------
  445.     normalizeParent() - Ensures that we always get the parent of a node of 
  446.         type NODE_ELEMENT or NODE_ATTRIBUTE.
  447. ------------------------------------------------------------------------------*/
  448. function normalizeParent(oEvent)
  449. {
  450.     // Return the node if it is of type NODE_ELEMENT or NODE_ATTRIBUTE;
  451.     if (oEvent.Source.nodeType == 1 || oEvent.Source.nodeType == 2)
  452.         return oEvent.Parent;
  453.         
  454.     // Return the node 's parent if the node is of type NODE_TEXT and its parent
  455.     // is of type NODE_ELEMENT
  456.     if (oEvent.Parent.nodeType == 1 && oEvent.Source.nodeType == 3)
  457.         return oEvent.Parent.parentNode;
  458. }
  459.  
  460. /*------------------------------------------------------------------------------
  461.     initializeNodeValue()
  462. ------------------------------------------------------------------------------*/
  463. function initializeNodeValue(xpath, strValue)
  464. {
  465.     var xmlNode = getNode(xpath);
  466.  
  467.     // Set the node value *ONLY* if the node is empty.
  468.     if (xmlNode.text == "")
  469.         setNodeValue(xmlNode, strValue);
  470. }
  471.  
  472. /* =============================================================================
  473.     Simple nodelist operations
  474. ============================================================================= */
  475.  
  476. /*------------------------------------------------------------------------------
  477.     getNode()
  478. ------------------------------------------------------------------------------*/
  479. function getNode(xpath)
  480. {
  481.     // Both XML node and absolute XPath are allowed.
  482.     if (typeof(xpath) == "string")
  483.         return XDocument.DOM.selectSingleNode(xpath);
  484.     else
  485.         return xpath;
  486. }
  487.  
  488. /*------------------------------------------------------------------------------
  489.     getNodeList()
  490. ------------------------------------------------------------------------------*/
  491. function getNodeList(xpath)
  492. {
  493.     // Both XML node and absolute XPath are allowed.
  494.     if (typeof(xpath) == "string")
  495.         return XDocument.DOM.selectNodes(xpath);
  496.     else
  497.         return xpath;
  498. }
  499.  
  500. /*------------------------------------------------------------------------------
  501.     count()
  502. ------------------------------------------------------------------------------*/
  503. function count(xpath)
  504. {
  505.     var xmlNodeList = getNodeList(xpath);
  506.  
  507.     if (xmlNodeList)
  508.         return xmlNodeList.length;
  509.     else
  510.         return -1;
  511. }
  512.  
  513. /* =============================================================================
  514.     Complex nodelist operations
  515. ============================================================================= */
  516.  
  517. /*------------------------------------------------------------------------------
  518.     applyAction() - Iterate the whole nodelist and apply a given action
  519.         to each node in the nodelist. You can implement your own actions.
  520.  
  521.     Example:
  522.         var oAction = new Object();
  523.         oAction.sum = 0;
  524.         oAction.apply = new Function("xmlNode", "this.sum += xmlNode.nodeTypedValue");
  525. ------------------------------------------------------------------------------*/
  526. function applyAction(xpath, oAction)
  527. {
  528.     var xmlNodeList = getNodeList(xpath);
  529.     var xmlNode;
  530.  
  531.     while (xmlNode = xmlNodeList.nextNode())
  532.         oAction.apply(xmlNode);
  533.  
  534.     xmlNodeList.reset();
  535. }
  536.  
  537. /*------------------------------------------------------------------------------
  538.     sum()
  539. ------------------------------------------------------------------------------*/
  540. function sum(xpath)
  541. {
  542.     var oAction = new Object;
  543.     oAction.nResult = 0;
  544.     oAction.apply = new Function("xmlNode", "this.nResult += getNodeTypedValue(xmlNode, 0, true)");
  545.  
  546.     applyAction(xpath, oAction);
  547.     return oAction.nResult;
  548. }
  549.  
  550. /*------------------------------------------------------------------------------
  551.     anyInvalidOrEmpty() - Searches a nodelist to see if there are any invalid 
  552.         or empty nodes.
  553. ------------------------------------------------------------------------------*/
  554. function anyInvalidOrEmpty(xpath)
  555. {
  556.     var oAction = new Object;
  557.     oAction.fInvalid = false;
  558.     oAction.apply = new Function("xmlNode", "this.fInvalid |= isInvalidOrEmpty(xmlNode)");
  559.     
  560.     applyAction(xpath, oAction);
  561.     return oAction.fInvalid;
  562. }
  563.  
  564. /*------------------------------------------------------------------------------
  565.     reindex()
  566. ------------------------------------------------------------------------------*/
  567. function reindex(xpath, iStart)
  568. {
  569.     if (iStart == null)
  570.         iStart = 1;
  571.  
  572.     var oAction = new Object;
  573.     oAction.i = iStart;
  574.     oAction.apply = new Function("xmlNode", "setNodeTypedValue(xmlNode, this.i++)");
  575.  
  576.     return applyAction(xpath, oAction);
  577. }
  578.  
  579. /*------------------------------------------------------------------------------
  580.     average()
  581. ------------------------------------------------------------------------------*/
  582. function average(xpath, xpathWeights)
  583. {
  584.     var oAction = new Object;
  585.     oAction.nSum = 0;
  586.     oAction.cNodes = 0;
  587.  
  588.     if (!xpathWeights)
  589.     {
  590.         oAction.apply = actionSimpleAverage;
  591.         oAction.result = resultSimpleAverage;
  592.     }
  593.     else
  594.     {
  595.         oAction.nWeights = 0;
  596.         oAction.xmlWeights = getNodeList(xpathWeights);
  597.         oAction.apply = actionWeightedAverage;
  598.         oAction.result = resultWeightedAverage;
  599.     }
  600.  
  601.     applyAction(xpath, oAction);
  602.     return oAction.result();
  603. }
  604.  
  605. /*------------------------------------------------------------------------------
  606.     actionSimpleAverage()
  607. ------------------------------------------------------------------------------*/
  608. function actionSimpleAverage(xmlNode)
  609. {
  610.     if (!isInvalidOrEmpty(xmlNode))
  611.     {
  612.         this.nSum += getNodeTypedValue(xmlNode, 0, true);
  613.         this.cNodes++;
  614.     }
  615. }
  616.  
  617. /*------------------------------------------------------------------------------
  618.     resultSimpleAverage()
  619. ------------------------------------------------------------------------------*/
  620. function resultSimpleAverage()
  621. {
  622.     if (this.cNodes)
  623.         return this.nSum / this.cNodes;
  624.     else
  625.         return 0;
  626. }
  627.  
  628. /*------------------------------------------------------------------------------
  629.     actionWeightedAverage()
  630. ------------------------------------------------------------------------------*/
  631. function actionWeightedAverage(xmlNode)
  632. {
  633.     // There should be one weight for each node. Even if the node is invalid,
  634.     // we have to advance the iterator of weights to keep them in sync.
  635.     var xmlWeight = this.xmlWeights.nextNode();
  636.  
  637.     if (!isInvalidOrEmpty(xmlNode) && !isInvalidOrEmpty(xmlWeight))
  638.     {
  639.         var nValue = getNodeTypedValue(xmlNode, 0, true);
  640.         var nWeight = getNodeTypedValue(xmlWeight, 1, true);
  641.  
  642.         this.nSum += nValue * nWeight;
  643.         this.nWeights += nWeight;
  644.         this.cNodes++;
  645.     }
  646. }
  647.  
  648. /*------------------------------------------------------------------------------
  649.     resultWeightedAverage()
  650. ------------------------------------------------------------------------------*/
  651. function resultWeightedAverage()
  652. {
  653.     this.xmlWeights.reset();
  654.  
  655.     if (this.cNodes)
  656.     {
  657.         if (this.nWeights > 0)
  658.             return this.nSum / this.nWeights;
  659.         else
  660.             return this.nSum
  661.     }
  662.     else
  663.     {
  664.         return 0;
  665.     }
  666. }
  667.  
  668. /*------------------------------------------------------------------------------
  669.     sort()
  670. ------------------------------------------------------------------------------*/
  671. function sort(xpathParent, xpathChildren, fnCompare)
  672. {
  673.     // Retrieve the parent node; all the children will be sorted.
  674.     var xmlOrigParent = getNode(xpathParent)
  675.     var xmlItems = xmlOrigParent.selectNodes(xpathChildren);
  676.  
  677.     if (1 < count(xmlItems))
  678.     {
  679.         // Store the (pointers to) items in an array for faster access.
  680.         rgItems = new Array();
  681.         
  682.         while (xmlItem = xmlItems.nextNode())
  683.             rgItems.push(xmlItem);
  684.             
  685.         // Sort the array (the XML does not change).
  686.         rgItems.sort(fnCompare);
  687.  
  688.         // Now that the array is sorted the DOM should be updated.
  689.         var xmlSortParent = xmlOrigParent.cloneNode(true);
  690.         var xmlClones = xmlSortParent.selectNodes(xpathChildren);
  691.         xmlClones.removeAll();
  692.         
  693.         // Update the nodelist, each item is moved only once.
  694.         // (each change is causing several notifications to be fired.)
  695.         for (var i=0; i<rgItems.length; i++)
  696.             xmlSortParent.appendChild(rgItems[i].cloneNode(true));
  697.         
  698.         xmlOrigParent.parentNode.replaceChild(xmlSortParent, xmlOrigParent);
  699.     }
  700. }
  701.